﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using PlanningGIS.ArcGIS;
using System.IO;
using PlanningGIS.Util;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Display;
using PlanningGIS.Engine.Controls;
using System.Drawing.Text;

namespace PlanningGIS.Symbol
{
    /// <summary>
    /// 
    /// </summary>
    public partial class TextStyle : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public TextStyle()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StyleCreator_Load(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// 符号构建处理类
        /// </summary>
        private StyleCreator _Creator = null;

        /// <summary>
        /// 已选择的符号对象
        /// </summary>
        public ISymbol SelectedSymbol { get; set; }

        /// <summary>
        /// 初始化控件
        /// </summary>
        public void InitControl()
        {
            if (_Creator == null)
            {
                InstalledFontCollection fonts = new InstalledFontCollection();
                int colCount = fonts.Families.GetUpperBound(0);
                for (int i = 0; i <= colCount; i++)
                {
                    cmbFont.Items.Add(fonts.Families[i].Name);
                }
                _Creator = new StyleCreator();
                _Creator.StylePanel = itmpnlStyles;
                _Creator.StyleCommand = cmdStyle;
                _Creator.AddFiles(cmbStyles);
            }
            try
            {
                UpdateSymbol();
                UpdateImage();
                _Creator?.SetSelected(SelectedSymbol);
            }
            catch
            {

            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateSymbol()
        {
            ITextSymbol sym = (ITextSymbol)SelectedSymbol;
            clrAnno.SelectedColor = Convertor.IColor2Color(sym.Color);
            iptASize.Value = sym.Size;
            cmbFont.SelectedText = sym.Font.Name;
            btnBold.Checked = sym.Font.Bold;
            btnItalic.Checked = sym.Font.Italic;
            btnUnderline.Checked = sym.Font.Underline;
            btnStrikethrough.Checked = sym.Font.Strikethrough;
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddStyle_Click(object sender, EventArgs e)
        {
            if (opdlgStyle.ShowDialog(this.ParentForm) != DialogResult.OK) return;
            _Creator?.CreateStyles(zjgisStyleGalleryType.TextSymbols, opdlgStyle.FileName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbStyles_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                _Creator?.CreateStyles(zjgisStyleGalleryType.TextSymbols, cmbStyles.SelectedValue.ToString());
            }
            catch
            {

            }
            
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdStyle_Executed(object sender, EventArgs e)
        {
            ButtonItem btn = (ButtonItem)sender;
            picPreview.Image = btn.Image;
            SelectedSymbol = (ISymbol)btn.Tag;
            UpdateSymbol();
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateImage()
        {
            picPreview.Image = SymbolHelper.DrawSymbol(SelectedSymbol, picPreview.Width, picPreview.Height);
        }
        
        #region 注记
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clrAnno_SelectChanged(object sender, ColorEventArgs e)
        {
            if (!(SelectedSymbol is ITextSymbol)) return;
            (SelectedSymbol as ITextSymbol).Color = Convertor.Color2IColor(clrAnno.SelectedColor);
            UpdateImage();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbFont_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!(SelectedSymbol is ITextSymbol)) return;
            ITextSymbol itSymbol = (SelectedSymbol as ITextSymbol);
            stdole.IFontDisp myFont = itSymbol.Font;
            myFont.Name = cmbFont.Text;
            UpdateImage();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void iptASize_ValueChanged(object sender, EventArgs e)
        {
            if (!(SelectedSymbol is ITextSymbol)) return;
            ITextSymbol itSymbol = (SelectedSymbol as ITextSymbol);
            stdole.IFontDisp myFont = itSymbol.Font;
            myFont.Size = (decimal)iptASize.Value;
            UpdateImage();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdFont_Executed(object sender, EventArgs e)
        {
            if (!(SelectedSymbol is ITextSymbol)) return;
            ITextSymbol itSymbol = (SelectedSymbol as ITextSymbol);
            stdole.IFontDisp myFont = itSymbol.Font;
            ButtonX btn = (ButtonX)sender;
            switch (btn.CommandParameter.ToString())
            {
                case "B":
                    myFont.Bold = btn.Checked;
                    break;
                case "I":
                    myFont.Italic = btn.Checked;
                    break;
                case "U":
                    myFont.Underline = btn.Checked;
                    break;
                case "S":
                    myFont.Strikethrough = btn.Checked;
                    break;
            }
            btn.Checked = !btn.Checked;
            UpdateImage();
        }
        #endregion

    }
}
